//import Vue from 'vue'
import axios from 'axios'
import { baseURL } from "./config";

/**
 * @description axios初始化
 */
const http = axios.create({
    baseURL,
    timeout: 30000,
    headers: {
      //'Content-Type': contentType,
    },
  })

//请求拦截器
http.interceptors.request.use(
    (config) => {
      // 可使用async await 做异步操作
       var token = localStorage.getItem("token");
    //   const token = store.getters['user/token']
  
      // if (!token) {
      //   token =
      //     "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwidHlwIjoiSldUIn0..G_m0uS_FTNy29d7Okdzu0g.02wzW5RApFHnTx6pAphNSNtGlKZ5mLodWlRv9suQPkqRfBWkHF6caYdjqFm1cyvnxFBtPdvi5d6EUnCHxM20s4iBbxlq9lr8GT37k_U4oDfBw4024SjV8S7mO6SP7zmOTKKqYKiNxpUBL5rp61pXHeYRK_oW5XtiDTMHQ4Us3UjxPU-fCWXbyk_QmjlDHmdZkBEd6GCZtTXMX14J1c82wZAc3Ps9TwVfkz3wGIcItv96SPzaa3XKp27TczM6vDxcN3lmaTjYsJQ6Rd5rxl2UjO42a0z__s9H4IzmhoDh7Et9MnBfd62FFM3mc7n384LdpYxhwJ8qO_bS1nydepab3H1zzlGFetHPU0f8Ck1ZMr5As6o9EExRO4lnKWFuJ4XC5ifw24xVwUtf5jVN9RjommARdLLGlgQHdeNz-SiYmZqBh8wxp1xkXFyEuZkPl2N-.XbJuTMqsONf9nHzLqXnvmp_m4udaFncxzGBqwh_Sgww";
      // }
  
      if (token) {
        config.headers["Authorization"] =  token;
      }
  
      if (config.method === "POST") {
        config.data = JSON.stringify(config.data);
      }
      return config;
    },
    (error) => {
      return Promise.resolve(error);
    }
  );
  
  // 响应拦截器
  http.interceptors.response.use(
    (response) => {
      //console.log(response);
      return response.data;
    },
    (error) => {
      //未登录时清空缓存跳转
      if (error.statusCode == 401) {
        localStorage.clear()
        //跳转
      }
      return Promise.resolve(error);
    }
  );
  export default http;